package com.situ.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.taglibs.standard.lang.jstl.test.beans.PublicBean1;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.ssm.entity.PageBean;
import com.situ.ssm.entity.Student;
import com.situ.ssm.service.IStudentService;
import com.situ.ssm.service.impl.StudentServiceImpl;
import com.situ.ssm.vo.StudentSearchCondition;

@Controller
@RequestMapping(value="/student")
public class StudentController {
	private IStudentService studentService = new StudentServiceImpl();
	
	//list
	@RequestMapping(value="/list")
	//@ResponseBody
	public String list(Model model) {
		//新建SpringMVC测试
		List<Student> list  = new ArrayList<Student>();
		/*list.add(new Student(1,"藏三",23,"男"));
		list.add(new Student(2,"藏四",24,"男"));
		list.add(new Student(3,"藏五",25,"男"));*/
		list = studentService.list();
		model.addAttribute("list",list);
		for (Student student : list) {
			System.out.println(student);
		}
		return "/student/student_list";
	}
	
	//添加1
	@RequestMapping(value="getInsertPage")
	public String getInsertPage() {
		return "/student/student_insert";
	}
	
	//添加
	@RequestMapping(value="insert")
	@ResponseBody
	public Boolean insert(String name,Integer age,String gender) {
		Student student = new Student(name,age,gender);
		boolean result = studentService.insert(student);
		return result;
	}
	
	//根据id删除
	@RequestMapping(value="deleteById")
	@ResponseBody
	public Boolean deleteById(Integer id) {
		boolean result =  studentService.deleteById(id);
		return result;
	}
	
	//修改前查询
	@RequestMapping(value="toUpdate")
	public String toUpdate(Integer id,Model model) {
		Student student = studentService.findById(id);
		model.addAttribute("student",student);
		System.out.println(student);
		return "student/student_update";//转发
	}
	
	//修改
	@RequestMapping(value="update")
	@ResponseBody
	public Boolean updata(Integer id, String name,Integer age,String gender) {
		Student student = new Student(id,name, age, gender);
		boolean result = studentService.update(student);
		return result;
	}
	
	//批量删除
	@RequestMapping(value="deleteAll")
	@ResponseBody
	public Boolean deleteAll(String[] selectIds) {
		boolean result = studentService.deleteAll(selectIds);
		return result;
	}
	
	//分页
	@RequestMapping(value="pageList")
	//@ResponseBody
	// public PageBean<Student> 
	public String pageList(StudentSearchCondition condition,Model model) {
		if (condition.getPageNo() == null) {
			condition.setPageNo(1);
		}
		if (condition.getPageSize() == null) {
			condition.setPageSize(3);
		}
		PageBean<Student> pageBean =  studentService.getPageBean(condition);
		model.addAttribute("pageBean",pageBean);
		//return pageBean;
		return "student/student_list";
	}
	
	//一对一查询
	@RequestMapping(value="oneVoneList")
	//@ResponseBody
	public String oneVoneList(Model model) {
		List<Student> list = studentService.oneVoneList();
		model.addAttribute("list",list);
		return "student/oneVone";
	}
}
